package com.slwish.xty.mvp.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.SubscribeModel;
import com.slwish.xty.mvp.contract.SubscribeListContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;


/**
 * 模块说明：预约Presenter
 *
 * @author 陈浩
 * @date 2017/12/1
 */
public class SubscribeListPresenter extends BasePresenter<SubscribeListContract.View> implements SubscribeListContract.Presenter {
    public SubscribeListPresenter(SubscribeListContract.View view) {
        super(view);
    }


    @Override
    public void querySubscribeByUserId(long userId, int page) {
        EasyHttp.post(UrlConfig.QUERY_SUBSCRIBE_LIST_URL)
                .params("userId", userId + "")
                .params("page", page + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        setError(R.string.query_server_option_failed);
                        if (getView() != null)
                            getView().querySubscribeFailed();
                    }

                    @Override
                    public void onSuccess(String json) {
//                        LogUtils.i("xx", "json--->" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
//                                return;
                            }
                            List<SubscribeModel> models = JSON.parseArray(data, SubscribeModel.class);
                            SubscribeListContract.View mview = getView();
                            if ( mview != null) {
                                mview.querySubscribeSuccess(models);
                            }
                        } catch (Exception e) {
//                            setError(R.string.query_server_option_failed);
                            if (getView() != null)
                                getView().querySubscribeFailed();
                        }
                    }
                });
    }
}
